<?php
function t($t) {
	return $t;
}
require_once('config_tinybrowser.php');
require_once('fns_tinybrowser.php');
require_once('security.php');
$foldernow = str_replace(array('../','..\\','./','.\\'),'',($tinybrowser['allowfolders'] && isset($_REQUEST['folder']) ? urldecode($_REQUEST['folder']) : ''));
$swf = (isset($_GET['swf']) ? $_GET['swf'] : '');

$browsepath = $tinybrowser['path']['image'].$foldernow;
$linkpath = $tinybrowser['link']['image'].$foldernow;
$thumbpath = ROOT_URL.'uploaded-files/'.$foldernow.'_thumbs/';
$sBaseThumbPath = S_BASE_URL.'uploaded-files/'.$foldernow.'_thumbs/';
$path = ROOT_URL.'uploaded-files/'.$foldernow;
$s_base_url_folder = S_BASE_URL.'uploaded-files/'.$foldernow;

if(isset($_GET['filename'])) {
	$file = jsUriDecode($_GET['filename']);
	$image_url = $thumbpath.jsUriDecode($_GET['filename']);
	$full_image_url = $path.jsUriDecode($_GET['filename']);
	$ext = end(explode('.', $file));
	if(file_exists($sBaseThumbPath.'_'.$file)) {
		$thumb_url = $thumbpath.'_'.$file;
	}
	else {
		if(file_exists(S_BASE_URL.'includes/admin/tinybrowser/img/icons/'.strtoupper($ext).'.png')) {
			$thumb_url = ROOT_URL.'includes/admin/tinybrowser/img/icons/'.strtoupper($ext).'.png';
		}
		else {
			$thumb_url = ROOT_URL.'includes/admin/tinybrowser/img/icons/DEFAULT.png';
		}
	}
	if(isImg($ext, true)) {
		$imginfo = getimagesize($s_base_url_folder.$file);
		$image_width = $imginfo[0];
		$image_height = $imginfo[1];
	}
	$image_size = fileSizeInfo(filesize($s_base_url_folder.$file));
	$title = basename($file, '.'.$ext);
	$image_mime_type = returnMIMEType($ext);
	$presets = "
	<input type='hidden' name='fname' id='fname' value='$title' />
	";
	if($swf == 'true') {
	$presets .= "<h3>Options</h3><p>There aren't any options for {$ext}
	files because they aren't needed. Just click \"I'm Done\" below.</p>
	<input type='hidden' id='swf' value='true' />
	<input type='hidden' id='url' value='{$path}{$file}' />
	<input type='hidden' id='width' value='{$image_width}' />
	<input type='hidden' id='height' value='{$image_height}' />
	";
	}
	elseif (!isImg($ext)) {
	$presets .= "
  <h3>Image/Text <span>(Optional) Select what you want to be shown on the page.</span></h3>
  <div class='image box'>
    <select name='tb_image_preset' id='tb_image_preset'>
		<option value=''>Choose an option</option>
		<option selected='selected' value='$path$file'>Filename</option>
	</select>
  </div>
  <h3>Link <span>Choosing one of these options allows you to link to the file you uploaded.</span></h3>
  <div class='link box'>
    <select name='tb_link_options' id='tb_link_options' onchange=\"if($('#tb_link_options').val() == 'custom') {
			$('#tb_link_custom').css('display', 'block');
		}
		else if($('#tb_link_custom').css('display') == 'block') {
			$('#tb_link_custom').css('display', 'none');
		}\">
		<option value=''>Don't Link</option>
		<option value='custom'>Custom URL</option>
		<option selected='selected' value='$path$file'>File</option>
	</select>
		<input type='text' value='http://' name='tb_link_custom' id='tb_link_custom' style='display: none;'/>
    <select name='tb_link_target' id='tb_link_target'>
		<option selected='selected' value=''>Default (Same window/tab)</option>
		<option value='_blank'>New Window (_blank)</option>
		<option value='_top'>Topmost Window (_top)</option>
		<option value='_self'>Same Window (_self)</option>
		<option value='_parent'>Parent Window (_parent)</option>
	</select>
	<input type='hidden' id='file' value='true' />
  </div>
  <div class='spacer'/>
</div>	
";
	}
	else {
	$presets .= "
  <h3>Image <span>Select an image size to be shown on the page.</span></h3>
  <div class='image box'>
    <select name='tb_image_preset' id='tb_image_preset'>
		<option value=''>Choose an image size</option>
		<option value='$path$file'>Original (image)</option>
		<option selected='selected' value='".$thumbpath."_$file'>Thumbnail (image)</option>
	</select>
  </div>
  <h3>Link <span>(Optional) Do you want the image to link somewhere?</span></h3>
  <div class='link box'>
    <select name='tb_link_options' id='tb_link_options' onchange=\"if($('#tb_link_options').val() == 'custom') {
			$('#tb_link_custom').css('display', 'block');
		}
		else if($('#tb_link_custom').css('display') == 'block') {
			$('#tb_link_custom').css('display', 'none');
		}\">
		<option value=''>Don't Link</option>
		<option value='custom'>Custom URL</option>
		<option selected='selected' value='$path$file'>Original (image)</option>
		<option value='".$thumbpath."_$file'>Thumbnail (image)</option>
	</select>
		<input type='text' value='http://' name='tb_link_custom' id='tb_link_custom' style='display: none;'/>
    <select name='tb_link_target' id='tb_link_target'>
		<option selected='selected' value=''>Default (Same window/tab)</option>
		<option value='_blank'>New Window (_blank)</option>
		<option value='_top'>Topmost Window (_top)</option>
		<option value='_self'>Same Window (_self)</option>
		<option value='_parent'>Parent Window (_parent)</option>
	</select>
  </div>
  <h3>Alignment <span>(Optional) Left will make text flow to the right of the image and vis-vera.</span></h3>
  <div class='alignment box'>
    <select name='tb_alignment' id='tb_alignment'>
		<option selected='selected' value=''>No alignment</option>
		<option value='left'>Align left</option>
		<option value='right'>Align right</option>
	</select>
  </div>
  <div class='spacer'/>
</div>	
";
}
}
?>
<h2>Insert</h2>
<div class="presets">
  <?php print $presets; ?>
  <div class="spacer"></div>
</div>
<div class="details">
  <img src="<?php print $thumb_url; ?>" alt="Thumbnail" />
  <table>
    <tbody>
      <tr>
        <th><?php print t('Title'); ?></th>
        <td><?php print $title; ?></td>
      </tr>
		<?php
			if(isImg($ext)) {
		?>
      <tr>
        <th><?php print t('Dimensions'); ?></th>
        <td><?php print $image_width; ?> x <?php print $image_height; ?> px</td>
      </tr>
		<?php
			}
		?>
      <tr>
        <th><?php print t('Mime type'); ?></th>
        <td><?php print $image_mime_type; ?></td>
      </tr>
      <tr>
        <th><?php print t('Size'); ?></th>
        <td><?php print $image_size; ?></td>
      </tr>
    </tbody>
  </table>
  <div class="options">
  <br />
    <a href="edit.php?type=image&action=rename&folder=<?php echo $foldernow; ?>" class="button edit">Rename</a>
    <a href="edit.php?type=image&action=delete&folder=<?php echo $foldernow; ?>" class="button edit">Delete</a>
		<?php
			if(isImg($ext)) {
		?>
    <a href="edit.php?type=image&action=resize&folder=<?php echo $foldernow; ?>" class="button edit">Resize</a>
    <a href="edit.php?type=image&action=rotate&folder=<?php echo $foldernow; ?>" class="button edit">Rotate</a>
		<?php
			}
		?>
  </div>
</div>
<div class="footer">
  <a href="#" class="button closeOverlay" style='float:right;margin-top:2px;'><?php print t('Close'); ?></a>
  <button class="button insert" style='padding:2px;'>I'm Done!</button>
</div>